package com.scu.bank.web;
import com.scu.bank.core.jo.Result;
import com.scu.bank.core.jo.ResultGenerator;
import com.scu.bank.model.pojo.UserRole;
import com.scu.bank.service.UserRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
* Created by pmeng on 2021/01/04.
*/
@RestController
@RequestMapping("/user/role")
public class UserRoleController {
    @Autowired
    private UserRoleService userRoleService;

    @PostMapping("/add")
    public Result add(@RequestBody UserRole userRole) {
        userRoleService.save(userRole);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody List<String> ids) {
        if (ids == null){
            return ResultGenerator.genFailResult("请至少选择一项数据!");
        }
        userRoleService.deleteByIds(ids);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody UserRole userRole) {
        userRoleService.update(userRole);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam String id) {
        UserRole userRole = userRoleService.getById(id);
        return ResultGenerator.genSuccessResult(userRole);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageInfo<UserRole> pageInfo = userRoleService.list(page, size);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("getRoleList")
    public Result getRoleList(){
        List<Map<String,String>> roles = userRoleService.getRoleList();
        return ResultGenerator.genSuccessResult(roles);
    }
}
